package com.sun.springCache;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringCacheMain {
	
	/**
	 * 测试缓存 一旦被缓存 就不会再次获取值了
	 */
	@Test
	public void test1() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-cache-anno.xml");//加载 spring配置文件
		PersonService service = context.getBean(PersonService.class);
		service.getPersonById("1");
		service.getPersonById("2");
		service.getPersonById("1");
		//1--被缓存了
		//2--被缓存了
	}
	
	/**
	 * 如果缓存被清理 那么会重新加载缓存
	 */
	@Test
	public void test2() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-cache-anno.xml");//加载 spring配置文件
		PersonService service = context.getBean(PersonService.class);
		service.getPersonById("1");
		service.getPersonById("2");
		service.getPersonById("1");
		service.clear();
		service.getPersonById("1");
		service.getPersonById("2");
	}
	
	@Test
	public void test3() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-cache-anno.xml");//加载 spring配置文件
		PersonService service = context.getBean(PersonService.class);
		service.getPersonById1("1");
		service.getPersonById1("2");
		service.getPersonById1("1");
		service.getPersonById1("2");
	}
	
	@Test
	public void test4() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-cache-anno.xml");//加载 spring配置文件
		PersonService service = context.getBean(PersonService.class);
		service.getPersonById2("1", "aa");
		service.getPersonById2("2", "aa");
		service.getPersonById2("1", "aa");
		service.getPersonById2("2", "aa");
	}
	
	@Test
	public void test5() {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-cache-anno.xml");//加载 spring配置文件
		PersonService service = context.getBean(PersonService.class);
		service.getPerson(new Person("1"));
		service.getPerson(new Person("2"));
		service.getPerson(new Person("1"));
		service.getPerson(new Person("2"));
	}
}
